package cn.com.zzl.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @description 我的第一个servlet
 * @author 朱正磊
 * @date 2017年9月10日 上午11:17:05
 */
public class FirstServlet extends HttpServlet {

	private static final long serialVersionUID = -6311103244765581183L;

	/**
	 * 说明： 为了简化代码，我们在doGet方法中统一调用doPost的方法
	 * @description 处理GET请求
	 * @param request 请求对象
	 * @param response 响应对象
	 * @throws ServletException Servlet异常
	 * @throws IOException IO流异常
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException {
		doPost(request, response);
	}
	
	/**
	 * @description 处理POST请求
	 * @param request 请求对象
	 * @param response 响应对象
	 * @throws ServletException Servlet异常
	 * @throws IOException IO流异常
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException {
		String method = request.getParameter("method");
		if (method.endsWith("add"))
			add(request, response);//新增
		else if (method.endsWith("delete"))
			delete(request, response);//删除
		else if (method.endsWith("query"))
			query(request, response);//查询
		else if (method.endsWith("update"))
			update(request, response);//更新
		else
			error(request, response);//错误页面
	}

	/**
	 * @description 新增
	 * @param request 请求对象
	 * @param response 响应对象
	 */
	private void add(HttpServletRequest request, HttpServletResponse response) {
		try {
			System.out.println("add");
			request.setAttribute("value", "新增数据成功");
			request.getRequestDispatcher("success.jsp").forward(request, response);
		} catch (Exception e) {
			e.printStackTrace();
		} 
	}
	
	/**
	 * @description 删除
	 * @param request 请求对象
	 * @param response 响应对象
	 */
	private void delete(HttpServletRequest request, HttpServletResponse response) {
		try {
			System.out.println("delete");
			request.setAttribute("value", "删除数据成功");
			request.getRequestDispatcher("success.jsp").forward(request, response);
		} catch (Exception e) {
			e.printStackTrace();
		} 
	}
	
	/**
	 * @description 查询
	 * @param request 请求对象
	 * @param response 响应对象
	 */
	private void query(HttpServletRequest request, HttpServletResponse response) {
		try {
			System.out.println("query");
			request.setAttribute("value", "查询数据成功");
			request.getRequestDispatcher("success.jsp").forward(request, response);
		} catch (Exception e) {
			e.printStackTrace();
		} 
	}
	
	/**
	 * @description 更新
	 * @param request 请求对象
	 * @param response 响应对象
	 */
	private void update(HttpServletRequest request, HttpServletResponse response) {
		try {
			System.out.println("update");
			request.setAttribute("value", "更新数据成功");
			request.getRequestDispatcher("success.jsp").forward(request, response);
		} catch (Exception e) {
			e.printStackTrace();
		} 
	}

	/**
	 * @description 错误页面
	 * @param request 请求对象
	 * @param response 响应对象
	 */
	private void error(HttpServletRequest request, HttpServletResponse response) {
		try {
			System.out.println("error");
			request.setAttribute("value", "您访问的页面不存在！");
			request.getRequestDispatcher("error.jsp").forward(request, response);
		} catch (Exception e) {
			e.printStackTrace();
		} 
	}
	
}




